<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
</head>
<style>
    .box{
        width: 600px;
        height: 500px;
        margin: 0 auto;
        border: 1px solid #000;
        background-color: aquamarine;
    }
    .box1 {
        width: 400px;
        height: 300px;
        margin: 0 auto;
    }
    .box2{
        width: 100px;
        height: 50px;
        margin: 100px auto;
    }
</style>
<body>
    <h1>轮播图</h1>
    <div class="box">
         <!-- 图片展示区域 -->
    <div class="box1">
        <img id="box1" src="1.png" alt="" width="400px">
    </div>
    <div class="box2">
          <!-- 前一张和后一张按钮 -->
    <button id="syz">上一张</button>
    <button id="xyz">下一张</button>
    </div>
    </div>

    <script>
        //自执行函数
        (function() {
            const sz = ["1.png", "2.png", "3.png"]; //存储图片
            let a1 = 0;
            const tupian = document.getElementById('box1');
            const syz = document.getElementById('syz');
            const xyz = document.getElementById('xyz');

            function xianshi(index) {
                if (sz.length === 0) return;
                tupian.src = sz[index];
                
            }

            function XYZ() {
                a1 = (a1 + 1) % sz.length;//下一张
               xianshi(a1);
            }
            function SYZ() {
                a1 = (a1 - 1 + sz.length) % sz.length;//上一张
                xianshi(a1);
            }
            syz.addEventListener('click', SYZ);
            xyz.addEventListener('click', XYZ);

            setInterval(XYZ, 3000);//3000毫秒
        })();
    </script>
</body>
</html>